<?php
class ControllerVoucherSearch extends Controller
{
	private $error = array();
	
	public function index()
	{
		$this->load->model("category/stock");
		$this->getList();
	}
	
	public function order()
	{
		$this->load->model("voucher/order");
		$this->load->model("voucher/orderdetail");
		$this->load->model("common/control");
		
		$this->data['ordertypename'] = array(
			"IMS" => "Import - Purchase Order",
			"IMC" => "Import - Customer Return",
			"EXC" => "Export - Sale Order",
			"EXS" => "Export - Return to Supplier"
		);
		
		$this->data['link'] = array(
			"IMS" => HTTP_SERVER."index.php?route=voucher/purchase/update&orderid=",
			"IMC" => HTTP_SERVER."index.php?route=voucher/customerreturn/update&orderid=",
			"EXC" => HTTP_SERVER."index.php?route=voucher/saleorder/update&orderid=",
			"EXS" => HTTP_SERVER."index.php?route=voucher/returnsupplier/update&orderid="
		);
		
		$ordertypes = array(
			0 => array("datafield" => "%", "datadisplay" => "All"),
			1 => array("datafield" => "IMS", "datadisplay" => "Import - Purchase Order"),
			2 => array("datafield" => "IMC", "datadisplay" => "Import - Customer Return"),
			3 => array("datafield" => "EXC", "datadisplay" => "Export - Sale Order"),
			4 => array("datafield" => "EXS", "datadisplay" => "Export - Return to Supplier")
		);
		
		if($this->request->post['search_ordertype'] == "")
		{
			$this->request->post['search_ordertype'] = "%";
		}
		
		$this->data['cb_ordertype'] = $this->model_common_control->getDataCombobox($ordertypes, "datadisplay", "datafield", $this->request->post['search_ordertype']);
		
		$this->data['search_ordertypedisplay'] = $ordertypes[$this->request->post['search_ordertype']];
		if($this->data['search_ordertypedisplay'] == "")
		{
			$this->data['search_ordertypedisplay'] = "All";
		}
		
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		$where = " AND ordertype like '".$this->request->post['search_ordertype']."' AND orderdate >= '".$search_fromdate."' AND orderdate <= '".$search_todate."' ";
		
		$this->data['orders'] = $this->model_voucher_orderdetail->searchOrder($where);
		
		
		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="voucher/report_orderlist.tpl";
		}
		else
		{
			$this->template="voucher/searchorder.tpl";
		}
		
		$this->layout="layout/function";
		
		$this->render();
		
	}
	
		
	public function voucher()
	{
		$this->load->model("voucher/voucher");
		$this->load->model("common/control");
		
		$this->data['accounttype'] = array();
		$vouchergroups_expense = array(
				0 => array("vouchergroupid" => "VEXSP", "vouchergroupname" => "Payments to suppliers for goods and services (Ex: Purchase Order)"),
				1 => array("vouchergroupid" => "VEXCR", "vouchergroupname" => "Payments to customers for goods and services (Ex: Return order)"),
				2 => array("vouchergroupid" => "VEXES", "vouchergroupname" => "Salary"),
				3 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Amortization (loss of intangible asset value over time)"),
				4 => array("vouchergroupid" => "VEXEO", "vouchergroupname" => "Office Operating cost / Purchase Assets (Ex: Electronic, Water, Buy assets for company using)"),
				5 => array("vouchergroupid" => "VEXSL", "vouchergroupname" => "DEBT/LOANS of partners"),
				6 => array("vouchergroupid" => "VEXCL", "vouchergroupname" => "DEBT/LOANS of customers"),
				7 => array("vouchergroupid" => "VEXEL", "vouchergroupname" => "DEBT/LOANS of employee"),
				8 => array("vouchergroupid" => "VEXSR", "vouchergroupname" => "RENT ASSETS or others from partners"),
				9 => array("vouchergroupid" => "VEXEM", "vouchergroupname" => "Marketing cost, public relationship"),
				10 => array("vouchergroupid" => "VEXCT", "vouchergroupname" => "Return the deposit for customer when they receive products"),
				11 => array("vouchergroupid" => "VEXCC", "vouchergroupname" => "Change money to customer"),
				12 => array("vouchergroupid" => "VEXEA", "vouchergroupname" => "Capital")
			);
			
		$vouchergroups_income = array(
				0 => array("vouchergroupid" => "VINCS", "vouchergroupname" => "Receipts from the sale of goods or services (Ex: Sale Order, Sale services )"),
				1 => array("vouchergroupid" => "VINEI", "vouchergroupname" => "Receipts from internal company (employee)"),
				2 => array("vouchergroupid" => "VINSR", "vouchergroupname" => "Receipts from the returning products to supplier"),
				3 => array("vouchergroupid" => "VINSL", "vouchergroupname" => "DPBT/LOANS of Suppliers"),
				4 => array("vouchergroupid" => "VINCL", "vouchergroupname" => "DPBT/LOANS of Customer"),
				5 => array("vouchergroupid" => "VINEL", "vouchergroupname" => "DPBT/LOANS of Employee"),
				6 => array("vouchergroupid" => "VINCD", "vouchergroupname" => "Receipts from customer deposit order/products"),
				7 => array("vouchergroupid" => "VINCH", "vouchergroupname" => "Receipts from customer change products"),
				8 => array("vouchergroupid" => "VINCO", "vouchergroupname" => "Receipts from customer for other activities"),
				9 => array("vouchergroupid" => "VINSC", "vouchergroupname" => "Supplier change money"),
				10 => array("vouchergroupid" => "VINEA", "vouchergroupname" => "Capital")
			);
			
		foreach($vouchergroups_expense as $item)
		{
			$this->data['accounttype'][$item['vouchergroupid']] = $item['vouchergroupname']; 
		}
		
		foreach($vouchergroups_income as $item)
		{
			$this->data['accounttype'][$item['vouchergroupid']] = $item['vouchergroupname']; 
		}
				
		
		
		$this->data['link'] = array(
			"EXPENSE" => HTTP_SERVER."index.php?route=voucher/voucher/expense&voucherid=",
			"INCOME" => HTTP_SERVER."index.php?route=voucher/voucher/income&voucherid="
		);
		
		$vouchertypes = array(
			0 => array("datafield" => "%", "datadisplay" => "All"),
			1 => array("datafield" => "EXPENSE", "datadisplay" => "Expense"),
			2 => array("datafield" => "INCOME", "datadisplay" => "Income")
		);
		
		if($this->request->post['search_vouchertype'] == "")
		{
			$this->request->post['search_vouchertype'] = "%";
		}
		
		$this->data['cb_vouchertype'] = $this->model_common_control->getDataCombobox($vouchertypes, "datadisplay", "datafield", $this->request->post['search_vouchertype']);
		
		$this->data['search_vouchertypedisplay'] = $ordertypes[$this->request->post['search_vouchertype']];
		if($this->data['search_vouchertypedisplay'] == "")
		{
			$this->data['search_vouchertypedisplay'] = "All";
		}
		
		$this->data['search_fromdate'] = $this->request->post['search_fromdate'];
		$this->data['search_todate'] = $this->request->post['search_todate']; 
		if($this->request->post['search_fromdate'] == "")
		{
			$this->data['search_fromdate'] = date("01/m/Y");
		}
		
		
		if($this->request->post['search_todate'] == "")
		{
			$this->data['search_todate'] = date("d/m/Y");
		}
		
		$search_fromdate = $this->date->formatMySQLDateQuery($this->data['search_fromdate'],"DMY","-");
		$search_todate = $this->date->formatMySQLDateQuery($this->data['search_todate'],"DMY","-");
		
		$where = " AND vouchertype like '".$this->request->post['search_vouchertype']."' AND voucherdate >= '".$search_fromdate."' AND voucherdate <= '".$search_todate."' ";
		
		$this->data['vouchers'] = $this->model_voucher_voucher->search($where);
		
		$this->id='content';
		
		if($this->request->post['display'] == "report")
		{
			$this->template="voucher/report_voucherlist.tpl";
		}
		else
		{
			$this->template="voucher/searchvoucher.tpl";
		}
		
		
		$this->layout="layout/function";
		
		$this->render();
		
	}

	public function deletevoucher() {
		
		$this->load->model("voucher/voucher");
		
		$voucherid = $this->request->get['voucherid'];
    	$this->model_voucher_voucher->delete($voucherid);
		

		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}

	public function deleteorder() {
		
		$this->load->model("voucher/order");
		
		$orderid = $this->request->get['orderid'];
    	$this->model_voucher_order->delete($orderid);
		

		$this->id="content";
		$this->template="common/output.tpl";
		$this->render();
  	}
}
?>